using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skywave.ComponentModel;

namespace Skywave.Web.UI.WebControls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:MaDate runat=server></{0}:MaDate>")]
    public class TextDate : WebControl
    {
        DropDownList ddlMonth;
        TextBox txtDay, txtYear, txtMonth;
        Label lblFixedYear;
        Table table1;

        public TextDate()
        {
            table1 = new Table();
            ddlMonth = new DropDownList();
            txtDay = new TextBox();
            txtYear = new TextBox();
            txtMonth = new TextBox();
            lblFixedYear = new Label();

            TableRow row1 = new TableRow();
            TableCell cell1;

            txtDay.Width = 20;
            txtYear.Width = 20;
            txtMonth.Width = 20;
            lblFixedYear.Text = FixedYear;
            lblFixedYear.Width = 15;

            cell1 = new TableCell();
            cell1.Controls.Add(txtDay);
            row1.Cells.Add(cell1);
            //this.Controls.Add(txtDay);
            cell1 = new TableCell();
            if (mMonthView == MonthViews.Name)
            {
                for (int int1 = 1; int1 <= 12; int1++)
                {
                    ddlMonth.Items.Add(new ListItem(DateTimeHelper.GetMonthName(DateTimeHelperCalendars.Persian, int1), int1.ToString()));
                }
                cell1.Controls.Add(ddlMonth);
                //this.Controls.Add(ddlMonth);
            }
            else
            {
                cell1.Controls.Add(txtMonth);
                //this.Controls.Add(txtMonth);
            }
            row1.Cells.Add(cell1);

            cell1 = new TableCell();
            cell1.Controls.Add(txtYear);
            row1.Cells.Add(cell1);

            cell1 = new TableCell();
            cell1.Controls.Add(lblFixedYear);
            row1.Cells.Add(cell1);

            table1.Rows.Add(row1);

            this.Controls.Add(table1);
        }

        private int Day
        {
            get
            {
                int r;
                return (int.TryParse(txtDay.Text, out r)) ? r : -1;
            }
            set { txtDay.Text = value.ToString(); }
        }

        private int Year
        {
            get
            {
                string year = (txtYear.Text == "") ? "" : FixedYear + txtYear.Text;
                int r;
                return (int.TryParse(year, out r)) ? r : -1;
            }
            set { txtYear.Text = value.ToString().Substring(FixedYear.Length); }
        }

        private int Month
        {
            get
            {
                if (mMonthView == MonthViews.Name)
                {
                    return (ddlMonth.SelectedIndex < 0) ? -1 : int.Parse(ddlMonth.SelectedValue);
                }
                else
                {
                    int r;
                    return (int.TryParse(txtMonth.Text, out r)) ? r : -1;
                }
            }
            set
            {
                if (mMonthView == MonthViews.Name)
                {
                    ddlMonth.SelectedValue = value.ToString();
                }
                else
                {
                    txtMonth.Text = value.ToString();
                }
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [Browsable(false)]
        public string ValueBindable
        {
            get
            {
                return Value.ToString();
            }
            set
            {
                if (value == "")
                    Value = null;
                else
                    Value = DateTime.Parse(value);
            }
        }

        /// <summary>
        /// Value Property
        /// </summary>
        [Bindable(false)]
        [Category("Appearance")]
        public DateTime? Value
        {
            get
            {
                RefreshDateFromText();
                return ValueInternal;
            }
            set
            {
                ValueInternal = value;
                if (ValueInternal == null || ValueInternal.Value.Year == 1)
                {
                    EmptyInputs();
                }
                else
                {
                    DateTimeHelper tmp = new DateTimeHelper(DateTimeHelperCalendars.Persian, ValueInternal.Value);
                    Year = tmp.Year;
                    Month = tmp.Month;
                    Day = tmp.Day;
                }
            }
        }

        /// <summary>
        /// ValueInternal Property
        /// </summary>
        private DateTime? ValueInternal
        {
            get
            {
                object obj1 = ViewState["ValueInternal"];
                return (obj1 == null) ? null : (DateTime?)obj1;
            }
            set
            {
                ViewState["ValueInternal"] = value;
            }
        }

        public bool IsNull()
        {
            return (Value == null || Value.Value.Year == 1);
        }

        private void EmptyInputs()
        {
            txtYear.Text = "";
            txtDay.Text = "";
            ddlMonth.SelectedIndex = -1;
            txtMonth.Text = "";
        }

        private void RefreshDateFromText()
        {
            //IN THIS FUNCTION WE SHOULD NOT HAVE ANY CALL TO IsNull or Value because it will create a dead end loop
            int dY = Year, dM = Month, dD = Day;
            if (dY <= 0 || dM <= 0 || dD <= 0)
            {
                Value = null;
                return;
            }
            DateTime tmp;
            DateTimeHelper tmpMa;
            if (!DateTimeHelper.IsValidDate(DateTimeHelperCalendars.Persian, dY, dM, dD))
            {
                throw new ArgumentException("The entered date is not valid.");
            }
            tmpMa = new DateTimeHelper(DateTimeHelperCalendars.Persian, DateTimeHelperCalendars.Persian, dY, dM, dD);
            tmp = tmpMa.DateTime.Value;
            if (ValueInternal != null && ValueInternal.Value.Year != 1)
            {
                DateTime curValue = ValueInternal.Value;
                ValueInternal = new DateTime(tmp.Year, tmp.Month, tmp.Day, curValue.Hour, curValue.Minute, curValue.Second, curValue.Millisecond);
            }
            else
                ValueInternal = tmp;
        }


        /// <summary>
        /// FixedYear Property
        /// </summary>
        public string FixedYear
        {
            get
            {
                object obj1 = ViewState["FixedYear"];
                return (obj1 == null) ? "13" : (string)obj1;
            }
            set
            {
                ViewState["FixedYear"] = value;
            }
        }

        public enum MonthViews
        {
            Name = 0, Number = 1
        }
        private MonthViews mMonthView = MonthViews.Number;

        public MonthViews MonthView
        {
            get { return mMonthView; }
            set { mMonthView = value; }
        }
    }
}
